/*action create by pionner code builder*/

package com.pioneer.app.sms.jiaotong;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.hibernate.Query;
import org.hibernate.Transaction;

import com.pioneer.app.dev.department.Department;

import config.BaseHibernateDAO;

public class NormalSmsDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(NormalSms.class);
	public NormalSms findById(java.lang.Integer id){
		log.debug("getting NormalSms instance with id: " + id);
		try {
			NormalSms instance =(NormalSms)getSession().get("com.pioneer.app.sms.jiaotong.NormalSms",id);
			return instance;
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void delete(NormalSms persistentInstance){
		log.debug("deletting NormalSms persistentInstance  ");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void add(NormalSms persistentInstance){
		log.debug("deletting NormalSms persistentInstance  ");
		try {
			getSession().save(persistentInstance);
			log.debug("add successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void update(NormalSms persistentInstance){
		log.debug("deletting NormalSms persistentInstance  ");
		try {
			getSession().update(persistentInstance);
			log.debug("update successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List findByCondition(String condition){
		log.debug("finding by condition "+condition);
		try {

			String queryString =null;
			if(null!=condition){
				queryString = "from NormalSms where "+condition;
			}else{queryString = "from NormalSms";
			}

			Query queryObject = getSession().createQuery(queryString);
			queryObject.setMaxResults(500);
			return queryObject.list();
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void deleteObjs(java.lang.Integer[]ids){
		log.debug("deleting by objects ");
		Transaction tx=this.getSession().beginTransaction();
		try {
			if(null!=ids){
				NormalSms obj=null;
				java.lang.Integer id=null;
				
				for(int i=0;i<ids.length;i++){
					id=ids[i];
					obj=this.findById(id);
					this.delete(obj);
				}
				tx.commit();
			}
		}catch (RuntimeException re) {
			log.error("get failed", re);
			tx.rollback();
			throw re;
		}
	}
	public Document findSelWinDoc(String condition){
		Document doc=null;
		
		try {
			List ls=this.findByCondition(condition);
			doc=DocumentFactory.getInstance().createDocument();
			Element root=doc.addElement("NODES");
			Iterator it=ls.iterator();
			Element elt=null;
			NormalSms obj=null;
			while(it.hasNext()){
				obj=(NormalSms)it.next();
				elt=root.addElement("NODE");
				elt.addAttribute("id", ""+obj.getId().intValue());
				elt.addAttribute("name", obj.getTitle());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return doc;
	}
}
